from bilibliwebutils import *
from concurrent.futures import ThreadPoolExecutor
from multiprocessing import cpu_count
import threading
import requests
import time

mthr_data_path = "./data_mthr/"

def thread_task(url):
    # print(str("%s threading url: %s") %(threading.current_thread().name, url))
    resp = requests.get(url)
    # print("《《" * 10 + url + "》》》" * 10)
    items = parse_rank_html(resp.text, True)
    file_path = mthr_data_path + url.split("/")[-1] + ".txt"
    write_local_data(items, file_path)

def mutil_thread():
    pool = ThreadPoolExecutor(max_workers=cpu_count(), thread_name_prefix="test_")
    for api in B_RANK_APIS:
        pool.submit(thread_task, B_RANK_URL + api)
    pool.shutdown()


if __name__ == "__main__":
    create_floder(mthr_data_path)
    start = time.time()
    mutil_thread()
    end = time.time()
    print("\n 》》》mutil thread cost:" + str(end - start) + " seconds")